package com.ray.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ray.bean.CompanyBean;
import com.ray.bean.CompanyMaterialCountBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.MaterialPositionBean;
import com.ray.bean.WarehouseInOutDetailBean;
import com.ray.mapper.CompanyMapper;
import com.ray.mapper.CompanyMaterialCountMapper;
import com.ray.mapper.MaterialPositionMapper;
import com.ray.mapper.OrderMapper;
import com.ray.util.ReturnStringUtil;
import com.ray.util.StringUtil;

import net.sf.json.JSONArray;

/**
 * 公司仓库库存逻辑层
* <p>Title: CompanyMaterialCountService</p>  
* <p>Description: </p>  
* @author 李斌  
* @date 2018年9月17日
 */
@Service
public class CompanyMaterialCountService {
	
	@Autowired
	CompanyMaterialCountMapper companyMaterialCountMapper;
	@Autowired
	OrderMapper orderMapper;
	@Autowired
	MaterialPositionMapper materialPositionMapper;
	@Autowired
	CompanyMapper companyMapper;
	
	@Autowired
	PurchaseOrderService purchaseOrderService;
	
	/**
	 * 获取库存信息
	* @date 2018年9月17日 上午9:54:05 
	* @author 李斌
	* @return List<CompanyMaterialCountBean> 
	* @throws
	 */ 
	public List<CompanyMaterialCountBean> getCompanyMaterialCountListByDetail(Map<String, Object> map) {
		return companyMaterialCountMapper.getCompanyMaterialCountListByDetail(map); 
	}
	
	/**
	 * 获取公司物料仓位
	 * @param map
	 * @return
	 */
	public List<MaterialPositionBean> getMaterialPositionList(Map map){
		return materialPositionMapper.getMaterialPositionList(map);
	}
	
	/**
	 * 分页查询库存
	* @date 2018年9月19日 下午3:01:06 
	* @author 李斌
	* @return DatatablesViewPageBean<CompanyMaterialCountBean> 
	* @throws
	 */
	public DatatablesViewPageBean<CompanyMaterialCountBean> getCompanyMaterialCountListByPage(Map<String, Object> map) {
		//获取公司物料库存信息
		List<CompanyMaterialCountBean> companyMaterialCountList = companyMaterialCountMapper.getCompanyMaterialCountListData(map);
	    //获取公司物料仓位，一个物料有多个
		List<MaterialPositionBean> materialPositionList = this.getMaterialPositionList(map);
		for(CompanyMaterialCountBean companyMaterialCount:companyMaterialCountList) {
			if(materialPositionList !=null && materialPositionList.size()>0) {
				//设置物料仓位
				List<String> positionList = materialPositionList.stream().filter(po->po.getMaterialCode().equals(companyMaterialCount.getMaterialCode())).map(MaterialPositionBean::getPosition).collect(Collectors.toList());
				companyMaterialCount.setPositionList(positionList);
			}
			//如果仓位是原料或者辅料仓,设置本物料所有投料单未领料数
			if(companyMaterialCount.getWarehouseType() == 4 || companyMaterialCount.getWarehouseType()==5) {
				Double materialUseCount = purchaseOrderService.getMaterialUseCount(companyMaterialCount.getMaterialCode(), map.get("loginCompany").toString());
				companyMaterialCount.setMaterialUseCount(materialUseCount);
			}
		}
		int count = companyMaterialCountMapper.getCompanyMaterialCountListCount(map);
		//获取公司仓位是否可修改
		CompanyBean companyBean= companyMapper.getCompany(map);
		DatatablesViewPageBean<CompanyMaterialCountBean> datatablesViewPageBean = new DatatablesViewPageBean<CompanyMaterialCountBean>();
		datatablesViewPageBean.setiTotalDisplayRecords(count);
		datatablesViewPageBean.setiTotalRecords(count);
		datatablesViewPageBean.setReturnData(companyMaterialCountList);
		datatablesViewPageBean.setCode(companyBean.getCanUpdateMaterialPosition());
		return datatablesViewPageBean;  
	}
	
	/**
	 * 获取导出Excel的数据
	 * @param map
	 * @return
	 */
	public List<CompanyMaterialCountBean> getExportExcelFileByCompanyMaterial(Map map){
		return companyMaterialCountMapper.getCompanyMaterialCountListData(map);
	}
	
	/**
	 * 
	 * @param map
	 * @return
	 */
	public DatatablesViewPageBean<CompanyMaterialCountBean> getOrderMaterialStockCount(Map map) {
		DatatablesViewPageBean<CompanyMaterialCountBean> datatablesViewPageBean=new DatatablesViewPageBean<CompanyMaterialCountBean>();
		List<CompanyMaterialCountBean> list=companyMaterialCountMapper.getOrderMaterialStockCountData(map);
		int total=companyMaterialCountMapper.getOrderMaterialStockCountDataCount(map);
		datatablesViewPageBean.setReturnData(list);
		datatablesViewPageBean.setiTotalDisplayRecords(total);
		datatablesViewPageBean.setiTotalRecords(total);
		return datatablesViewPageBean;
	}
	
	
	/**
	 * 更改库存通过物料代码和仓库
	* @date 2018年9月17日 上午10:07:48 
	* @author 李斌
	* @return int 
	* @throws
	 */
	public void updateCompanyMaterialCountOnCountByMaterialCodeAndWarehouseType(CompanyMaterialCountBean companyMaterialCountBean) {
		int row = companyMaterialCountMapper.updateCompanyMaterialCountOnCountByMaterialCodeAndWarehouseType(companyMaterialCountBean);
		Assert.isTrue(row>0,"更新库存失败");
	}
	
	/**
	 * 新增库存信息
	* @date 2018年9月17日 下午4:31:58 
	* @author 李斌
	* @return void 
	* @throws
	 */
	public void addCompanyMaterialCount(CompanyMaterialCountBean companyMaterialCountBean) {
		Map<String, Object> unitMaterialMap = new HashMap<String,Object>();
		unitMaterialMap.put("loginCompany", companyMaterialCountBean.getCompany());
		unitMaterialMap.put("warehouseType", companyMaterialCountBean.getWarehouseType());
		unitMaterialMap.put("materialCode", companyMaterialCountBean.getMaterialCode());
		unitMaterialMap.put("orderId", companyMaterialCountBean.getOrderId());
		unitMaterialMap.put("rowIndex", companyMaterialCountBean.getRowIndex());
		this.duplicateCheck(unitMaterialMap);
		int row = companyMaterialCountMapper.addCompanyMaterialCount(companyMaterialCountBean);
		Assert.isTrue(row>0,"新增库存失败");
	}
	
	/**
	 * 获取原材料和辅料的合计
	* @date 2018年9月18日 上午11:42:22 
	* @author 李斌
	* @return void 
	* @throws
	 */
	public CompanyMaterialCountBean getCompanyMaterialCountByMaterial(Map<String, Object> map) {
		return companyMaterialCountMapper.getCompanyMaterialCountByMaterial(map);
	}
	
	/**
	 * 通过出入库记录获取物料库存
	* @date 2018年9月20日 上午8:09:13 
	* @author 李斌
	* @return void 
	* @throws
	 */
	public List<CompanyMaterialCountBean> getCompanyMaterialStackByWarehouseInOut(Map<String, Object> map) {
		return companyMaterialCountMapper.getCompanyMaterialStackByWarehouseInOut(map);
	}
	
	/**
	 * 获取库存物料的总额
	* @date 2018年9月21日 上午11:28:48 
	* @author 李斌
	* @return Double 
	* @throws
	 */
	public Double getCompanyMaterialCountPrice(Map<String, Object> map) {
		return companyMaterialCountMapper.getCompanyMaterialCountPrice(map);
	}

	/**
	 * 查重
	* @date 2018年10月5日 下午5:16:53 
	* @author 李斌
	* @return List<CompanyMaterialCountBean> 
	* @throws
	 */
	public void duplicateCheck(Map<String, Object> unitMaterialMap) {
		List<CompanyMaterialCountBean> companyMaterialCountBeans = companyMaterialCountMapper.duplicateCheck(unitMaterialMap);
		Assert.isTrue(companyMaterialCountBeans ==null || companyMaterialCountBeans.size()<1, "库存重复");
	}

	/**
	 * 设置成品单价
	 * jsonArray
	 * orderId 订单编号  rowIndex 行号  unitPrice 单价
	 * @param map
	 * @return
	 */
	public String setFinishUnitPrice(Map map) {
		StringUtil.checkIsTrue(map, "jsonArray", "未获取到参数");
		String jsonStr=map.get("jsonArray").toString();
		JSONArray jsonArray=JSONArray.fromObject(jsonStr);
		Assert.isTrue(jsonArray.size()>0,"未获取到参数");
		List<CompanyMaterialCountBean> list=(List<CompanyMaterialCountBean>) JSONArray.toCollection(jsonArray, CompanyMaterialCountBean.class);
		int  row=companyMaterialCountMapper.setFinishUnitPriceByList(list);
		return ReturnStringUtil.SAVE_SUCCESS;
	}
}
